<template>
  <div class="dashboard-layout">
    <el-container>
      <el-header>
        <div class="logo-box">
<!--          <el-icon class="collapse-icon" @click="handleChangeMenuCollapse">-->
<!--            <i class="iconfont icon-liebiao"></i>-->
<!--          </el-icon>-->
<!--          <el-divider direction="vertical" />-->
<!--          <span class="logo-text" @click="handleGotoIndex">{{$t('appName')}}</span>-->
          <div class="logo" @click="handleGotoIndex"></div>
        </div>
<!--        <div class="search-box">-->
<!--          <el-input-->
<!--              clearable-->
<!--              class="header-search-input"-->
<!--              v-model="searchText"-->
<!--              size="large"-->
<!--              placeholder="输入关键字搜索"-->
<!--              :suffix-icon="Search"-->
<!--          />-->
<!--        </div>-->
        <div class="menu-box">
          <div class="menu-item" :class="currentPath==='/ds/record' ? 'active' : ''" @click="gotoRoute('/ds/record')">用量统计</div>
        </div>
        <div class="user-box">
          <el-dropdown @command="handleDropMenuClick">
            <div class="user-info">
              <span class="name">{{userInfo.userAccount}}</span>
              <el-icon class="el-icon--right">
                <arrow-down />
              </el-icon>
            </div>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item command="info-change">个人信息修改</el-dropdown-item>
                <el-dropdown-item command="password-change">修改密码</el-dropdown-item>
                <el-dropdown-item command="logout">退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </el-header>
      <el-container>
        <el-main class="dashboard-main"><router-view/></el-main>
      </el-container>
<!--      <el-footer>-->
<!--        <footer-comp></footer-comp>-->
<!--      </el-footer>-->
    </el-container>
  </div>
  <el-dialog title="用户信息修改" width="30%" v-model="userInfoDialogVisible">
    <div class="user-info-content">
      <div class="form-row">
        <span class="label">联系方式</span>
        <el-input placeholder="请输入联系方式" v-model="userInfoForm.contact"></el-input>
      </div>
      <div class="form-row">
        <span class="label">备注</span>
        <el-input placeholder="请输入备注" v-model="userInfoForm.remark"></el-input>
      </div>
    </div>
    <template #footer>
      <div>
        <el-button link @click="userInfoDialogVisible = false">取消</el-button>
        <el-button type="primary" :loading="saveUserInfoLoading" @click="handleSaveUserInfo">确定</el-button>
      </div>
    </template>
  </el-dialog>
  <el-dialog title="密码修改" width="30%" v-model="passwordDialogVisible">
    <div class="user-info-content">
      <div class="form-row">
        <span class="label">新密码</span>
        <el-input placeholder="请输入新密码" v-model="userInfoForm.password"></el-input>
      </div>
    </div>
    <template #footer>
      <div>
        <el-button link @click="passwordDialogVisible = false">取消</el-button>
        <el-button type="primary" :loading="savePasswordLoading" @click="handleSaveNewPassword">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
import {ref, provide, onMounted} from 'vue'
import {Fold, Expand, ArrowDown, Search } from '@element-plus/icons-vue'
import {ElMessage} from 'element-plus'
import FooterComp from './components/footer.vue'
import {useRouter} from "vue-router";
import api from './api/index.js'
import common from "~/common/common.js";
import {md5} from "js-md5";

const user = common.getLocalUser()
const router = useRouter()
window.__router = router
const isCollapse = ref(false)
onMounted(() => {
  document.body.classList.remove('index-body')
})
const handleChangeMenuCollapse = () => {
  isCollapse.value = !isCollapse.value
}

const searchText = ref('')

const currentPath = ref(router.currentRoute.value.fullPath)

const userInfoForm = ref({
  contact: '',
  remark: '',
  password: '',
})
const userInfo = ref({})
const getUserInfo = () => {
  api.getUserInfo(user.userId).then((res) => {
    if (res.code === 0) {
      userInfo.value = res.data
      userInfoForm.value.contact = res.data.contact
      userInfoForm.value.remark = res.data.remark
    }
  })
}

const handleGotoIndex = () => {
  // router.replace('/index')
}
const gotoRoute = (path) => {
  currentPath.value = path
  router.push(path)
}

const userInfoDialogVisible = ref(false)
const passwordDialogVisible = ref(false)
const handleDropMenuClick = (command) => {
  switch(command) {
    case 'info-change':
      getUserInfo()
      userInfoDialogVisible.value = true
      break;
    case 'password-change':
      userInfoForm.value.password = ''
      passwordDialogVisible.value = true
      break;
    case 'logout':
      common.removeLocalUser()
      common.removeLocalToken()
      router.replace('/login')
      break;
  }
}
let avatarFile = null
const handleFileChange = (evt) => {
  const file = evt.target.files[0]
  avatarFile = file
  let fileResult = ''
  const reader = new FileReader()
  reader.readAsDataURL(file)
  reader.onload = () => {
    fileResult = reader.result
  }
  reader.onloadend = () => {
    userInfoForm.value.icon = fileResult
    // console.log('handleFileChange', fileResult)
  }
}
const saveUserInfoLoading = ref(false)
const handleSaveUserInfo = () => {
  if (!userInfoForm.value.contact && !userInfoForm.value.remark) {
    ElMessage.error('联系方式和备注至少填写一个')
    return
  }
  saveUserInfoLoading.value = true
  api.updateUserInfo({
    userId: userInfo.value.userId,
    contact: userInfoForm.value.contact,
    remark: userInfoForm.value.remark,
  }).then(res => {
    saveUserInfoLoading.value = false
    userInfoDialogVisible.value = false
    if (res.code === 0) {
      ElMessage.success('保存成功')
    } else {
      ElMessage.error('保存失败')
    }
  }).catch(e => {
    saveUserInfoLoading.value = false
    ElMessage.error('保存异常')
    console.log(e)
  })
}
const savePasswordLoading = ref(false)
const handleSaveNewPassword = () => {
  if (!userInfoForm.value.password) {
    ElMessage.error('请输入密码')
    return
  }
  if (userInfoForm.value.password.length < 8) {
    ElMessage.error('密码长度至少8位')
    return
  }
  savePasswordLoading.value = true
  api.updateUserPassword({
    userId: userInfo.value.userId,
    userPassword: md5(userInfoForm.value.password),
  }).then(res => {
    savePasswordLoading.value = false
    passwordDialogVisible.value = false
    if (res.code === 0) {
      ElMessage.success('密码修改成功')
      common.removeLocalUser()
      common.removeLocalToken()
      router.replace('/login')
    } else {
      ElMessage.error('密码修改失败')
    }
  }).catch(e => {
    savePasswordLoading.value = false
    ElMessage.error('密码修改异常')
    console.log(e)
  })
}
getUserInfo()
</script>

<style scoped lang="scss">
  .dashboard-layout{
    position: relative;
    width: 100%;
    height: 100%;
  }
  .el-container{
    position: relative;
    width: 100%;
    height: 100%;
    overflow: hidden;
    background: transparent;
  }
  .el-header{
    position: relative;
    display: flex;
    align-items: center;
    justify-content: space-between;
    height: 80px;
    .logo-box{
      display: flex;
      align-items: center;
      font-size: 2.4rem;
    }
    .logo{
      background: url("./assets/images/logo.png") no-repeat;
      background-size: 100% 100%;
      width: 144px;
      height: 52px;
      cursor: pointer;
    }
    .menu-box{
      position: absolute;
      display: flex;
      align-items: center;
      left: 15%;
      height: 100%;
      top:0;
      .menu-item{
        position: relative;
        font-size: 1.2rem;
        cursor: pointer;
        margin-right: 4rem;
        display: inline-block;
        color: var(--el-text-color-primary);
        &:after{
          content: '';
          position: absolute;
          top: calc(100% + 5px);
          width: 0;
          right: 0;
          height: 3px;
          -webkit-transition: width .2s cubic-bezier(0.29, 0.18, 0.26, 0.83);
          -o-transition: width .2s cubic-bezier(0.29, 0.18, 0.26, 0.83);
          transition: width .2s cubic-bezier(0.29, 0.18, 0.26, 0.83);
          background: var(--primary-color);
        }

      }
      .menu-item:hover:after,.menu-item.active:after{
        background: var(--primary-color);
        width: 100%;
        left: 0;
      }
    }
    .logo-text{
      cursor: pointer;
    }
    .collapse-icon{
      cursor: pointer;
    }
    .icon-liebiao{
      font-size: 2.2rem;
    }
    .header-search-input{
      width: 32rem;
    }
  }
  .el-aside{
    //background: rgb(60 64 73);
  }
  .el-menu{
    height: 100%;
    border-right: none;
    //background: none;
    background: rgb(60 64 73);
  }
  .el-menu-item{
    color: #fff;
    &:hover{
      background-color: rgb(56 159 238);
    }
    &.is-active{
      color: #fff;
      background-color: rgb(56 159 238);
    }
  }
  .user-box{
    display: flex;
    align-items: center;
    justify-content: space-between;

    .icon-xiaoxitixing{
      font-size: 2rem;
      color: var(--el-text-color-secondary);
    }
    .el-divider{
      margin: 0 20px;
    }
  }
  .dashboard-main{
    background: #f5f5f9;
    padding: 0;
  }
  .user-info-content{
    position: relative;
    //padding: 2rem 3.5rem;
  }
  .form-row{
    position: relative;
    margin-top: 20px;
    display: flex;
    align-items: center;
    .label{
      width: 80px;
      min-width: 80px;
    }
  }
  .el-footer{
    height: 50px;
    padding: 0;
  }
</style>
